//
//  USMemorialDayDetailVC.m
//  us
//
//  Created by lhx on 2025/4/30.
//

#import "USMemorialDayDetailVC.h"
#import "USAddMemorialDayController.h"
#import "USMemorialPreviewVC.h"
@interface USMemorialDayDetailVC ()

@end

@implementation USMemorialDayDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    if (self.dayModel) {
        [self setupView];

    }else if (self.anniversaryId.length > 0) {
        @weakify(self)
        [self getDailyViewWithID:self.anniversaryId success:^{
         
            @strongify(self)
          
            [self setupView];

        }];
    }
    
}
 

- (void)setupView {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage systemImageNamed:@"ellipsis"] style:UIBarButtonItemStylePlain target:self action:@selector(moreAction:event:)];
    
    UIButton *sureBtn = [UIButton us_fillButtonWithTitle:@"分享"];
    [sureBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.height.mas_equalTo([UIButton us_buttonDefaultHeight]);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-12);
    }];
    
    self.backgroundView.backgroundColor = [UIColor clearColor];
    self.backgroundView.layer.cornerRadius = 14.f;
    self.backgroundView.layer.masksToBounds = YES;
    self.backgroundView.backgroundColor = [UIColor mi_1_color];
    

    [self reloadUI];
}

- (void)reloadUI {
    self.titleLab.text = self.dayModel.anniversaryName;
    
    self.infoLab.text = self.dayModel.dayDescription;
//    self.infoLab.text = [self.dayModel.dayDescription stringByAppendingString:self.dayModel.weekDay];

    
    UIColor *color = [UIColor jk_colorWithHexString:self.dayModel.color];
    self.topView.backgroundColor = [color colorWithAlphaComponent:0.5];
    
    if (self.dayModel.coverUrl.length == 0) {
        self.coverImageView.image = [UIImage systemImageNamed:@"circle.fill"];
    }else{
        self.coverImageView.image = [UIImage imageNamed:self.dayModel.coverUrl];
    }
    
    self.coverImageView.tintColor = color;
    self.titleLab.adjustsFontSizeToFitWidth = YES;
    self.titleLab.textColor = [color jk_darkenColor:0.35];
    self.contentLab.attributedText = [self.dayModel dayAttributeWithFontSize:52];
}
 
#pragma mark - action
- (void)moreAction:(UIBarButtonItem *)sender event:(UIEvent *)event{
    
    [USFeedbackTool impactMedium];
        
    DTItem *editItem = [DTItem itemWithTitle:@"编辑" image:[UIImage imageNamed:@"icon-edit-22"] tag:0 identifier:nil handle:^(DTItem *item) {
        [self editAction];
    }];
    
    DTItem *deleteItem = [DTItem itemWithTitle:@"删除" image:[UIImage imageNamed:@"icon-trash-22"] tag:0 identifier:nil handle:^(DTItem *item) {
        [self deleteAction];
        
    }];
    
    NSArray *items = @[editItem,deleteItem];
    if (self.dayModel.togetherFlag) {
        items = @[editItem];
    }
    

    DTTargetView *view = [DTTargetView initWithBarItem:sender item:items];

    [view show];
    
}
- (void)editAction {
    USAddMemorialDayController *vc = [USAddMemorialDayController new];
    vc.dayModel = self.dayModel;
    vc.isEdit = YES;
    @weakify(self)
    vc.editSuccess = ^{
        @strongify(self)
        [self reloadUI];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - net
- (void)getDailyViewWithID:(NSString *)anniversaryId success:(void(^)(void))success {
    [[EMNetWork shareManager] em_post:API_AnniversaryView
                               params:@{@"anniversaryId":anniversaryId?:@""}
                                  hud:nil
                            showError:NO
                              success:^(EMApiModel *api) {
        
        if (!api.success) {
            return;
        }
        USMemorialDayModel *model = [USMemorialDayModel mj_objectWithKeyValues:api.data[@"anniversary"]];
        self.dayModel = model;
        
        success();
    } fail:^{
        
        
    }];
}
- (void)deleteAction {
    MOAlertBaseView *alert = [[MOAlertBaseView alloc]initWithTitle:@"确定删除此纪念日吗" detailTitle:@"此操作不可撤销哦，请谨慎操作" contentView:nil leftButtonImageName:nil rightButtonImageName:nil autoDissmiss:YES tapDissmiss:YES];
    alert.titleLabel.textColor = UIColor.us_redColor;
    @weakify(self)
    alert.didClickIndexBlock = ^(NSInteger index) {
        @strongify(self)
        if (index == 1) {
            [self deleteDay];
        }
    };
    
    [alert show];

    
}
- (void)deleteDay {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"anniversaryId"] = self.dayModel.anniversaryId;
    
    [[EMNetWork shareManager] em_post:API_AnniversaryDelete
                               params:params
                                  hud:nil
                            showError:YES
                              success:^(EMApiModel *api) {
        
        
        if (!api.success) {
            return;
        }
        [USFeedbackTool notifySuccess];
        [self.navigationController popViewControllerAnimated:YES];
        
    } fail:^{
        
        
    }];
}
- (void)shareAction:(id)sender{
    
    [USFeedbackTool impactSoft];
    
    USMemorialPreviewVC *vc = [[USMemorialPreviewVC alloc]init];
    vc.dayModel = self.dayModel;
    [self.navigationController pushViewController:vc animated:YES];
 
}

@end
